<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\ServerManager;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Carbon\Carbon;

class ServerManagerController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ServerManager(), function (Grid $grid) {
            $grid->model()->orderBy('show','desc')->orderBy('expire');
            // $grid->column('id')->sortable();
            $grid->column('name')->style('font-weight:bold;')->editable();
            $grid->column('ip')->editable();
            $grid->column('configuration')->editable();
            $grid->column('run_state')->switch();
            $grid->column('pay_type')->using([1 => '包年包月', 0 => '按量付费'])->dot([
                'default' => 'primary', // 设置默认颜色，不设置则默认为 default
                0 => 'danger',
                1 => 'success',
            ]);
            $grid->column('expire')->sortable()->display(function($v){
                if($v<= Carbon::now()){
                    return "<span style='color:red;font-weight:bold;'>$v</span>";
                }
                else if($v<= Carbon::now()->addDays(3)){
                    return "<span style='color:blue;font-weight:bold;'>$v</span>";
                }else{
                    return "<span style='color:limegreen;font-weight:bold;'>$v</span>";
                }
            })->editable()->filter(
               Grid\Column\Filter\Between::make()->datetime()
            );
            $grid->column('show')->switch();
            $grid->column('platform')->editable();
            $grid->column('platform_account')->editable();
            $grid->disableFilterButton();
            $grid->disableBatchActions();
            $grid->disableDeleteButton();
            $grid->disableActions();
            $grid->quickSearch(['name', 'expire','ip']);
            $grid->addTableClass(['table-text-center']);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ServerManager(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('ip');
            $show->field('configuration');
            $show->field('run_state');
            $show->field('pay_type');
            $show->field('expire');
            $show->field('show');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ServerManager(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->text('ip');
            $form->text('configuration');
            $form->select('run_state')->options([1=>'运行中',0=>'停止运行'])->default(1);
            $form->select('pay_type')->options([1=>'包年包月',0=>'按量付费'])->default(1);
            $form->datetime('expire');
            $form->text('platform');
            $form->text('platform_account');
            $form->switch('show')->default(1);
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
